//
//  SLOtherLoginController.m
//  MyChatDemo
//
//  Created by DragonYao on 2017/9/10.
//  Copyright © 2017年 DragonYao. All rights reserved.
//

#import "SLOtherLoginController.h"

@interface SLOtherLoginController ()

@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftConstraint;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@property (weak, nonatomic) IBOutlet UITextField *userTF;
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;

@end

@implementation SLOtherLoginController

- (void)dealloc {
    SLog(@"controller dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
        self.leftConstraint.constant = 23.0;
        self.rightConstraint.constant = 23.0;
    }
}


- (IBAction)registerAction:(UIButton *)sender {
    
    if ([SLUtils isNilString:self.userTF.text]) {
        [ProgressHUD showError:@"请输入电话号码"]; return;
    }
    if ([SLUtils isNilString:self.pwdTF.text]) {
        [ProgressHUD showError:@"请输入登录密码"]; return;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [UserLocalInfo sharedUserInfo].userId = self.userTF.text;
    [UserLocalInfo sharedUserInfo].userPwd = self.pwdTF.text;
    
    __weak typeof(self) weakSelf = self;
    [[XMPPManager sharedManager] userLogin:^(XMPPResultType type) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
            switch (type) {
                case XMPPResultTypeLoginSuccess: {
                    [UserLocalInfo sharedUserInfo].hasLogin = YES;
                    [weakSelf dismissViewControllerAnimated:NO completion:nil];
                    //切换到主控制器
                    UIStoryboard *mainStorebord = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    weakSelf.view.window.rootViewController = [mainStorebord instantiateInitialViewController];
                }
                    break;
                case XMPPResultTypeLoginFailure:
                    [ProgressHUD showError:@"电话号码或者登录密码错误！"];
                    break;
                case XMPPResultTypeNetError:
                    [ProgressHUD showError:@"网络出现问题！"];
                    break;
                default:
                    break;
            }
        });
    }];
}
- (IBAction)cancelRegisterAction:(UIBarButtonItem *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
